/*!
\copyright  Copyright (c) 2020-2022 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief   Implementations for the LE BAP Unicast server interfaces.
*/

#include "unicast_server_role.h"
#include "unicast_server_role_advertising.h"
#include "pacs_utilities.h"

#include <gatt_handler_db_if.h>
#include <logging.h>

#ifdef USE_SYNERGY
#include "cm_lib.h"
#include "bap_server_lib.h"
#else
#include "bap_server.h"
#endif

#define UNICAST_SERVER_ROLE_LOG     DEBUG_LOG

#define BAP_SERVER_DATA_PATH_ID_SHADOWING_PERIPHERAL       0xFA

ServiceHandle bapUnicastServiceHandle = 0;

void LeBapUnicastServer_Init(uint8 number_of_ases, Task bap_handler)
{
    BapServerHandleRange pacsServiceHandle = {HANDLE_PUBLISHED_AUDIO_CAPABILITIES_SERVICE,
                                              HANDLE_PUBLISHED_AUDIO_CAPABILITIES_SERVICE_END};
    BapServerHandleRange ascsServiceHandle = {HANDLE_ASCS_SERVICE, HANDLE_ASCS_SERVICE_END};

    PanicNull((void *) bap_handler);

#ifdef USE_SYNERGY
    bapUnicastServiceHandle = BapServerUnicastInit(TrapToOxygenTask(bap_handler),
                               number_of_ases, &pacsServiceHandle, &ascsServiceHandle);
    UNICAST_SERVER_ROLE_LOG("LeBapUnicastServer_Init bapUnicastServiceHandle=0x%x", bapUnicastServiceHandle);
#else
    bapUnicastServiceHandle = BapServerUnicastInit(bap_handler, number_of_ases,
                                                   &pacsServiceHandle, &ascsServiceHandle);
#endif
    LeBapPacsSetBapHandle(bapUnicastServiceHandle);

    LeBapPacsUtilities_Init();

    LeBapUnicastServer_SetupLeAdvertisingData();
}

void LeBapUnicastServer_CreateDataPath(uint16 cis_handle, bool host_to_controller, bool controller_to_host,
                                       bool is_cis_delegated)
{
    BapServerSetupDataPathReq dataPathParameters = {0};

    dataPathParameters.isoHandle = cis_handle,
    dataPathParameters.dataPathId = is_cis_delegated ? BAP_SERVER_DATA_PATH_ID_SHADOWING_PERIPHERAL:
                                                       BAP_SERVER_DATA_PATH_ID_RAW_STREAM_ENDPOINTS_ONLY;

    if (host_to_controller)
    {
        dataPathParameters.dataPathDirection = BAP_SERVER_DATA_PATH_DIRECTION_HOST_TO_CONTROLLER;
        BapServerSetupIsoDataPathReq(bapUnicastServiceHandle, BAP_ISO_UNICAST, &dataPathParameters);
    }

    if (controller_to_host)
    {
        dataPathParameters.dataPathDirection = BAP_SERVER_DATA_PATH_DIRECTION_CONTROLLER_TO_HOST;
        BapServerSetupIsoDataPathReq(bapUnicastServiceHandle, BAP_ISO_UNICAST, &dataPathParameters);
    }
}
